package com.mrxu.monitor.notice.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.mrxu.monitor.config.MonitorProperties;
import com.mrxu.monitor.notice.NotifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class FeiShuNotifyService implements NotifyService {

    @Autowired
    private MonitorProperties properties;

    // https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot
    @Override
    public void sendMsg(String content) {
        if(StrUtil.isBlank(properties.getNotify().feiShuWebhook)) {
            return;
        }
        log.info("发送飞书消息通知:{}",content);
        JSONObject message = new JSONObject();
        message.put("msg_type","text");
        JSONObject contentJson = new JSONObject();
        contentJson.put("text",content+"<at user_id=\"all\">所有人</at>");
        message.put("content",contentJson);
        String result = HttpUtil.post(properties.getNotify().feiShuWebhook,message.toJSONString());
        log.info("发送飞书消息通知返回:{}",result);
    }

}